package cn.gailvlun.gll.chatkit.record;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.gailvlun.gll.chatkit.R;



public class RecordViewView extends LinearLayout implements IRecordView {
    public RecordViewView(Context context) {
        this(context, null);
    }

    public RecordViewView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RecordViewView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    private ImageView iv;
    private TextView tvCount;
    private TextView tv;

    private RecordButton.RecordState mRecordState;

    private void init(Context context) {
        setGravity(Gravity.CENTER_HORIZONTAL);
        setOrientation(VERTICAL);
        setBackgroundResource(R.drawable.eight_radius_40_black);
        inflate(context, R.layout.record_view, this);

        iv = findViewById(R.id.iv);
        tvCount = findViewById(R.id.tv_count);
        tv = findViewById(R.id.tv);

        setVoiceState(RecordButton.RecordState.IDLE);
        if (isInEditMode()) {
            setVoiceState(RecordButton.RecordState.RECORDING);
        }
    }

    @Override
    public void setVoiceLevel(int level) {
        if (mRecordState == RecordButton.RecordState.RECORDING) {
            switch (level) {
                case 1: {
                    iv.setImageResource(R.drawable.icon_voice_one);
                    break;
                }
                case 2: {
                    iv.setImageResource(R.drawable.icon_voice_two);
                    break;
                }
                case 3: {
                    iv.setImageResource(R.drawable.icon_voice_three);
                    break;
                }
            }
        }



    }

    private void setCountDownByState(RecordButton.RecordState recordState) {
        switch (recordState) {
            case RECORDING: {
                iv.setVisibility(GONE);
                tvCount.setVisibility(VISIBLE);
                break;
            }
            case CANCELLING: {
                iv.setVisibility(VISIBLE);
                tvCount.setVisibility(GONE);
                break;
            }
        }
        tvCount.setText(String.valueOf(mCountDownTime));
    }

    @Override
    public void setVoiceState(RecordButton.RecordState state) {
        switch (state) {
            case IDLE: {
                isCountingDown = false;
                iv.setVisibility(VISIBLE);
                tvCount.setVisibility(GONE);
                setVisibility(GONE);
                break;
            }
            case RECORDING: {
                if (mRecordState != state) {
                    setVisibility(VISIBLE);
                    iv.setImageResource(R.drawable.icon_voice_one);
                    tv.setText("手指上滑，取消发送");
                    tv.setBackground(null);
                    if (isCountingDown) {
                        setCountDownByState(state);
                    }
                }
                break;
            }
            case CANCELLING: {
                if (mRecordState != state) {
                    setVisibility(VISIBLE);
                    iv.setImageResource(R.drawable.release_to_cancel);
                    tv.setText("松开手指，取消发送");
                    tv.setBackgroundResource(R.drawable.four_radius_brick);
                    if (isCountingDown) {
                        setCountDownByState(state);
                    }
                }
                break;
            }
            case SHORT: {
                if (mRecordState != state) {
                    setVisibility(VISIBLE);
                    iv.setVisibility(GONE);
                    tvCount.setVisibility(VISIBLE);
                    tvCount.setText("!");
                    tv.setText("说话时间太短");
                }
                break;
            }
        }
        mRecordState = state;
    }

    private boolean isCountingDown = false;
    private long mCountDownTime;

    @Override
    public void setCountDown(Long countDownTime) {
        isCountingDown = true;
        mCountDownTime = countDownTime;
        setCountDownByState(mRecordState);
        tvCount.setText(String.valueOf(countDownTime));
    }
}
